import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/page/sub/WebViewPage.dart';
import 'package:im_app/im/page/view/LoadingView.dart';
import 'package:im_app/im/page/view/MovableView.dart';
import 'package:im_app/im/util/ImConstant.dart';

import '../pojo/ImPojo.dart';
import 'bloc/WorkbenchCubit.dart';

class WorkbenchPage extends StatelessWidget {

  const WorkbenchPage({super.key});

  @override
  Widget build(BuildContext context) {
    var workbenchCubit = context.watch<WorkbenchCubit>();
    var modules = workbenchCubit.state.modules;
    var historyModule = workbenchCubit.state.historyModule;

    return LoadingView(
        state: workbenchCubit.state.state,
        child: _buildBody(context, modules, historyModule)
    );
  }

  Widget _buildBody(BuildContext context, List<ImModule> modules, ImModule? historyModule) {
    return BlocConsumer<WorkbenchCubit, WorkbenchState>(listener: (BuildContext context, WorkbenchState state) {
      var moduleItem = state.selectModuleItem;
      if (moduleItem != null) {
        if (moduleItem.type == ImModuleItemType.page) {
          Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context) {
            return WebViewPage(url: moduleItem.url!);
          }));
        } else {
          Navigator.of(context).pushNamed(moduleItem.url!, arguments: moduleItem.params);
        }
      }
    }, builder: (BuildContext context, WorkbenchState state) {
      return ListView(
        physics: const BouncingScrollPhysics(),
        children: _buildModules(context, modules, historyModule),
      );
    }, );
  }

  List<Widget> _buildModules(BuildContext context, List<ImModule> modules, ImModule? historyModule) {
    List<Widget> result = <Widget>[];
    if (historyModule != null && (historyModule.moduleItemList!.length ?? 0) > 0) {
      Widget historyWidget = _buildHistory(context, historyModule);
      result.add(historyWidget);
    }
    for (var module in modules) {
      if ((module.moduleItemList!.length ?? 0) > 0) {
        Widget moduleWidget = _buildModule(context, module);
        result.add(moduleWidget);
      }
    }
    return result;
  }

  Widget _buildModule(BuildContext context, ImModule module) {
    int length = module.moduleItemList!.length;
    print("gridview length: $length");
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: const EdgeInsets.only(top: 10, left: 10, bottom: 10),
          child: Text(
            module.name,
            style: const TextStyle(fontWeight: FontWeight.w500, fontSize: 16),
          ),
        ),
        Padding(

          padding: const EdgeInsets.only(left: 30),
          child: Wrap(
            direction: Axis.horizontal,
            spacing: 30,
            children: module.moduleItemList!.map((moduleItem) => _buildGridCell(context, moduleItem)).toList(),
          ),
        )
      ],
    );
  }

  Widget _buildHistory(BuildContext context, ImModule historyModule) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Padding(
          padding: EdgeInsets.only(top: 10, left: 10, bottom: 10),
          child: Text('历史记录', style: TextStyle(fontWeight: FontWeight.w500, fontSize: 16),),
        ),
        Padding(
          padding: const EdgeInsets.only(left: 30),
          child: Wrap(
            direction: Axis.horizontal,
            spacing: 30,
            children: historyModule.moduleItemList!.map((moduleItem) => _buildGridCell(context, moduleItem)).toList(),
          ),
        )
      ],
    );
  }

  Widget _buildGridCell(BuildContext context, ImModuleItem moduleItem) {
    return InkWell(
      child: Container(
        width: 80,
        height: 80,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [Icon(Icons.account_circle_rounded, size: 50), Text(moduleItem.name)],
        ),
      ),
      onTap: () {
        var workbenchCubit = context.read<WorkbenchCubit>();
        workbenchCubit.add(WorkbenchRouterEvent(moduleItem));
      },
    );
  }
}
